﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration.Claims;
using Microsoft.SharePoint.Administration;

namespace Riolinx.SocialConnekt.ManageTrustedLoginProvider
{
    class Program
    {
        private const string DELETE_OPERATION = "delete";
        private const string ADD_OPERATION = "add";

        static void Main(string[] args)
        {
            try
            {


                if (args.Length > 3)
                {
                    string strOperation = args[0];
                    string strLoginProviderName = args[1];
                    string strWebApplicationUrl = args[2];
                    string strZone = args[3];

                    if (strOperation == "addclaims")
                    {
                        SPSecurityTokenServiceManager mgr = SPSecurityTokenServiceManager.Local;

                        SPTrustedLoginProviderCollection providers = mgr.TrustedLoginProviders;
                        foreach (SPTrustedLoginProvider p in providers)
                        {
                            if (p.DisplayName.ToLower() == strLoginProviderName.ToLower())
                            {


                                p.AddClaimTypeInformation(new SPTrustedClaimTypeInformation("Gender", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender"));
                                p.Update();
                                Console.WriteLine("Additional Claims added");
                                Console.ReadLine();
                            }
                        }
                        return;
                    }

                    SPUrlZone zone = SPUrlZone.Custom;
                    switch (strZone)
                    {
                        case "Default":
                            zone = SPUrlZone.Default;
                            break;
                        case "Extranet":
                            zone = SPUrlZone.Extranet;
                            break;
                        case "Internet":
                            zone = SPUrlZone.Internet;
                            break;
                        case "Intranet":
                            zone = SPUrlZone.Intranet;
                            break;
                        default: zone = SPUrlZone.Custom;
                            break;
                    }



                    //SPFarm farm = SPFarm.Local;
                    SPWebApplication webApp = SPWebApplication.Lookup(new Uri(strWebApplicationUrl));

                    if (webApp != null)
                    {
                        bool bSuccess = false;
                        Dictionary<SPUrlZone, SPIisSettings> dict = webApp.IisSettings;
                        SPIisSettings iisSettings;
                        if (dict.TryGetValue(zone, out iisSettings))
                        {
                            if (strOperation == DELETE_OPERATION)
                            {

                                IEnumerable<SPAuthenticationProvider> authProviders = iisSettings.ClaimsAuthenticationProviders;
                                //first check if Windows Authentication is enabled
                                bool isWindowsAuthEnabled = false;
                                foreach (SPAuthenticationProvider authProvider in authProviders)
                                {
                                    if (authProvider.GetType() == typeof(SPWindowsAuthenticationProvider))
                                    {
                                        isWindowsAuthEnabled = true;
                                        break;
                                    }
                                }

                                if (!isWindowsAuthEnabled)
                                    iisSettings.AddClaimsAuthenticationProvider(new SPWindowsAuthenticationProvider());

                                foreach (SPAuthenticationProvider provider in iisSettings.ClaimsAuthenticationProviders)
                                {
                                    if (provider.DisplayName == strLoginProviderName)
                                    {
                                        try
                                        {

                                            iisSettings.DeleteClaimsAuthenticationProvider(provider);
                                            webApp.Provision();
                                            webApp.Update();
                                            bSuccess = true;
                                            break;
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine("There was an error while deleting the trusted provider: " + ex.ToString());
                                        }

                                    }
                                }
                            }

                            else if (strOperation == ADD_OPERATION)
                            {
                                SPTrustedAuthenticationProvider socialConnekt = new SPTrustedAuthenticationProvider(strLoginProviderName);

                                iisSettings.AddClaimsAuthenticationProvider(socialConnekt);
                                webApp.Provision();
                                webApp.Update();
                                Console.WriteLine(string.Format("The Trusted Login Provider named {0} was successfully added to web app {1} zone {2}.", strLoginProviderName, strWebApplicationUrl, strZone));
                                Console.ReadLine();
                            }
                        }

                        if (bSuccess)
                        {
                            SPSecurityTokenServiceManager mgr = SPSecurityTokenServiceManager.Local;

                            SPTrustedLoginProviderCollection providers = mgr.TrustedLoginProviders;
                            foreach (SPTrustedLoginProvider p in providers)
                            {
                                if (p.DisplayName.ToLower() == strLoginProviderName.ToLower())
                                {
                                    p.Delete();
                                    Console.WriteLine(string.Format("The Trusted Login Provider named {0} was deleted.", strLoginProviderName));
                                    Console.ReadLine();
                                    break;
                                }
                            }


                        }
                    }



                }
                else
                {
                    Console.WriteLine("Usage: Riolinx.SocialConnekt.DeleteTrustedLoginProvider.exe <Operation=Delete|Add> <ProviderName> <WebApplicationUrl> <Zone>");
                    Console.ReadLine();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("An error occurred: {0}.", ex.ToString()));
                Console.ReadLine();
            }
        }
    }
}
